package au.com.bluelink.scjp.chap7;

import java.util.*;

public class CollectionTest {

	public static void main(String[] args) {

		System.out.println("Tseting TestArrayList...");
		TestArrayList.test();

		System.out.println("Testing TestSort1...");
		TestSort1.test();

		System.out.println("Testing DVDInfo sorting...");

		// set up some DVD's
		DVDInfo dvd1 = new DVDInfo("Donnie Darko", "sci-fi", "Gyllenhall, Jake");
		DVDInfo dvd2 = new DVDInfo("Raiders of the Lost Ark", "action",
				"Ford, Harrison");
		DVDInfo dvd3 = new DVDInfo("2001", "sci-fi", "??");
		DVDInfo dvd4 = new DVDInfo("Caddy Shack", "comedy", "Murray, Bill");
		DVDInfo dvd5 = new DVDInfo("Star Wars", "sci-fi", "Ford, Harrison");
		DVDInfo dvd6 = new DVDInfo("Lost in Translation", "comedy",
				"Murray, Bill");
		DVDInfo dvd7 = new DVDInfo("Patriot Games", "action", "Ford, Harrison");

		ArrayList<DVDInfo> dvdList = new ArrayList<DVDInfo>();
		dvdList.add(dvd1);
		dvdList.add(dvd2);
		dvdList.add(dvd3);
		dvdList.add(dvd4);
		dvdList.add(dvd5);
		dvdList.add(dvd6);
		dvdList.add(dvd7);

		System.out.println("Before sorting dvdList...");
		System.out.println(dvdList);

		Collections.sort(dvdList);

		System.out.println("After sorting dvdList...");
		System.out.println(dvdList);

		GenreSort gs = new GenreSort();
		Collections.sort(dvdList, gs);

		System.out.println("After sorting dvdList by Genre...");
		System.out.println(dvdList);

		System.out.println("Testing SearchObjArray...");
		SearchObjArray.test();

		System.out
				.println("Testing ItTest(for List iteration using an Iterator)...");
		ItTest.test();
		
		System.out.println("Testing SetTest...");
		SetTest.test();

	}

}

class TestArrayList {
	public static void test() {
		List<String> test = new ArrayList<String>();
		String s = "hi";
		test.add("string");
		test.add(s);
		test.add(s + s);
		System.out.println(test.size());
		System.out.println(test.contains(42));

		System.out.println(test.contains("hihi"));
		test.remove("hi");
		System.out.println(test.size());
	}
}

class TestSort1 {
	public static void test() {
		ArrayList<String> stuff = new ArrayList<String>(); // #1
		stuff.add("Denver");
		stuff.add("Boulder");
		stuff.add("Vail");
		stuff.add("Aspen");
		stuff.add("Telluride");
		System.out.println("unsorted " + stuff);
		Collections.sort(stuff); // #2
		System.out.println("sorted " + stuff);
	}
}

class DVDInfo implements Comparable<DVDInfo> {
	String title;
	String genre;
	String leadActor;

	DVDInfo(String t, String g, String a) {
		this.title = t;
		this.genre = g;
		this.leadActor = a;
	}

	public String toString() {
		return this.title + " " + this.genre + " " + this.leadActor + "\n";
	}

	public int compareTo(DVDInfo d) {
		return this.title.compareTo(d.getTitle());
	}

	public String getTitle() {
		return this.title;
	}

	public String getGenre() {
		return this.genre;
	}

}

class GenreSort implements Comparator<DVDInfo> {
	public int compare(DVDInfo one, DVDInfo two) {
		return one.getGenre().compareTo(two.getGenre());
	}
}

class SearchObjArray {
	public static void test() {
		String[] sa = { "one", "two", "three", "four" };
		Arrays.sort(sa); // #1
		for (String s : sa)
			System.out.print(s + " ");
		System.out.println("\none = " + Arrays.binarySearch(sa, "one")); // #2
		System.out.println("now reverse sort");
		ReSortComparator rs = new ReSortComparator(); // #3
		Arrays.sort(sa, rs);
		for (String s : sa)
			System.out.print(s + " ");
		System.out.println("\none = " + Arrays.binarySearch(sa, "one")); // #4
		System.out.println("one = " + Arrays.binarySearch(sa, "one", rs)); // #5
	}

	static class ReSortComparator implements Comparator<String> { // #6
		public int compare(String a, String b) {
			return b.compareTo(a); // #7
		}
	}
}

class Dog {
	public String name;

	Dog(String n) {
		name = n;
	}
}

class ItTest {
	public static void test() {
		List<Dog> d = new ArrayList<Dog>();
		Dog dog = new Dog("aiko");
		d.add(dog);
		d.add(new Dog("clover"));
		d.add(new Dog("magnolia"));
		Iterator<Dog> i3 = d.iterator(); // make an iterator
		while (i3.hasNext()) {
			Dog d2 = i3.next(); // cast not required
			System.out.println(d2.name);
		}
		System.out.println("size " + d.size());
		System.out.println("get1 " + d.get(1).name);
		System.out.println("aiko " + d.indexOf(dog));
		d.remove(2);
		Object[] oa = d.toArray();
		for (Object o : oa) {
			Dog d2 = (Dog) o;
			System.out.println("oa " + d2.name);
		}
	}
}

class SetTest {
	public static void test() {
		boolean[] ba = new boolean[5];

		Set s = new HashSet();
		// Set s = new TreeSet(); // this won't compile! (objects must be
		// mutually compariable and different object types are not!)

		ba[0] = s.add("a");
		ba[1] = s.add(new Integer(42));
		ba[2] = s.add("b");
		ba[3] = s.add("a");
		ba[4] = s.add(new Object());
		
		for (int x = 0; x < ba.length; x++)
			System.out.print(ba[x] + " ");
		System.out.println("\n");
		for (Object o : s)
			System.out.print(o + " ");
	}
}